home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / PacMan / HighScoreTable.m < prev    next >
Encoding:
Text File  |  1992-08-10  |  5.9 KB  |  214 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "HighScoreTable.h"
  5. #import "PreferencesBrain.h"
  6. #import "GameBrain.h"
  7. #import <stdio.h>        // strcpy
  8. #import <stdlib.h>        // malloc
  9. #import <strings.h>
  10. #import <objc/typedstream.h>    // highscore tables
  11. #import <appkit/defaults.h>    // NXArgv
  12. #import <appkit/Matrix.h>     // matrix controls' methods
  13. #import <appkit/Control.h>     // various controls' methods
  14. #import <appkit/Cell.h>
  15. #import <appkit/Panel.h>    // Alert Panels
  16. #import <appkit/Application.h>    // application stuff
  17.  
  18. @implementation HighScoreTable
  19.  
  20. - init        // designated initializer sets up game variables
  21. {        // to sensible values.
  22.     int i;
  23.     char *slashPos;    // for filename fixing.
  24.     
  25.     for (i=0; i<MAXSCORES; i++) {  // get space to store names of high scorers
  26.         highNames[i] = malloc(256);
  27.     }
  28.     
  29.     if( !highScorePanel)
  30.       [NXApp loadNibSection:"HighScore.nib" owner:self withNames:NO];
  31.     
  32.     defaultPlayerName = malloc(64);
  33.     strcpy(defaultPlayerName, "Nobody");
  34.     scoreFileName = malloc(strlen(NXArgv[0]) + 16);
  35.     
  36.     // get name of highscore file (inside app wrapper)
  37.     
  38.     strcpy(scoreFileName, NXArgv[0]);
  39.     if (slashPos = strrchr(scoreFileName, '/')) {
  40.     sprintf(slashPos + 1, "\0");
  41.     } else {
  42.         strcpy(scoreFileName, "./");
  43.     }
  44.     strcat (scoreFileName, "highscores");
  45.  
  46.     [self readHighScores];
  47.     
  48.     return self;
  49. }
  50.  
  51. /* methods to get at important variables */
  52. - highScorePanel { return highScorePanel; }
  53. - setHighScorePanel:(id)panel { highScorePanel = panel; return self; }
  54. - highScoreDispMatrix { return highScoreDispMatrix; }
  55. - setHighScoreDispMatrix:(id)matrix {highScoreDispMatrix=matrix; return self;}
  56. - (int)highScore:(int)num { return highScores[num]; }
  57. - (const char *)defaultPlayerName { return defaultPlayerName; }
  58.  
  59. - setDefaultPlayerName:(const char *)name
  60. {
  61.     strcpy(defaultPlayerName, name);
  62.     return self;
  63. }
  64.  
  65. - findHighScoreText:sender        // used by below method to update cells
  66. {
  67.     int tag = [sender tag];
  68.     
  69.     if ((tag % 2) == 0) {    // name
  70.         [sender setStringValue:highNames[tag / 2]];
  71.     } else {            // score
  72.         [sender setIntValue:highScores[tag / 2]];
  73.     }
  74.     return self;
  75. }
  76.  
  77. - showHighScores            // update display matrix
  78. {
  79.     return [highScoreDispMatrix sendAction:@selector(findHighScoreText:)
  80.             to:self forAllCells:YES];
  81. }
  82.  
  83. - zeroHighScores            // wipes out high scores
  84. {
  85.     int i;
  86.     char *tempStr;
  87.     
  88.     for (i=0; i<10; i++) {
  89.         highScores[i] = (10 - i) * 100;
  90.     tempStr = malloc(16);
  91.     sprintf(tempStr, "Nobody #%d", i + 1);
  92.     strcpy(highNames[i], tempStr);
  93.     free(tempStr);
  94.     }
  95.     [self showHighScores];
  96.     return self;
  97. }
  98.  
  99. - putInHighScores:(int)score      // put player in high score table if applicable
  100. {
  101.     int i, j;
  102.     id nameText;
  103.     
  104.     for (i=0; i<10; i++) {
  105.         if (score >= highScores[i]) break;
  106.     }
  107.     if (i == 10) {
  108.         [self displayHighScores:self]; // high score panel out
  109.         return self; // not a new high score
  110.     }
  111.     for (j=9; j>i; j--) { // shift old scores down
  112.         strcpy(highNames[j],highNames[j-1]);
  113.         highScores[j] = highScores[j-1];
  114.     }
  115.     highScores[i] = score;
  116.     strcpy(highNames[i],[preferences defaultPlayerName]);
  117.     // set up textfield to accept player's name
  118.     if (highCell != nil) { // still hasn't entered previous name, so lost
  119.         // the chance -- we put in default now.
  120.         [highCell setBackgroundGray:NX_LTGRAY];
  121.         [highCell setBezeled:NO];
  122.         [highCell setEditable:NO];
  123.         highCell = nil;
  124.     }
  125.     nameText = [highScoreDispMatrix findCellWithTag:i*2];
  126.     [nameText setBackgroundGray:NX_WHITE];
  127.     [nameText setEditable:YES];
  128.     [nameText setBezeled:YES];
  129.     [nameText setStringValue:highNames[i]];
  130.     highCell = nameText;
  131.     
  132.     [self showHighScores];  // update matrix (like doing displayHighScores:)
  133.     [highScorePanel makeKeyAndOrderFront:self];  // want user to enter name
  134.     [highScoreDispMatrix selectCellWithTag:i*2]; 
  135.     return self;
  136. }
  137.  
  138. - displayHighScores:sender     // bring up high scores w/info loaded into it
  139. {
  140.     [self showHighScores];
  141.     [highScorePanel orderFront:self];
  142.     return self;
  143. }
  144.  
  145. - readHighScores            // read scores from file 
  146. {
  147.     NXTypedStream *typedStream;
  148.     int i; char *tmpStr;
  149.     FILE *testFile;
  150.  
  151.     [self zeroHighScores];
  152.     testFile = fopen(scoreFileName, "r"); // see if file exists; circumvents
  153.     if (testFile == NULL) return self;  // bug in NXOpenTypedStreamForFile()
  154.     fclose(testFile);
  155.     typedStream = NXOpenTypedStreamForFile(scoreFileName, NX_READONLY);
  156.     if (typedStream == NULL) { // Why doesn't this work?
  157.         NXRunAlertPanel("readHighScores","No highscore file.",NULL,NULL,"OK");
  158.         return self;
  159.     }
  160.     for (i=0; i<10; i++) {
  161.         NXReadTypes(typedStream, "i*", &highScores[i], &tmpStr);
  162.         strcpy(highNames[i], tmpStr);
  163.     }
  164.     NXCloseTypedStream(typedStream);
  165.  
  166.     return self;
  167. }
  168.  
  169. - writeHighScores            // write scores to file 
  170. {
  171.     NXTypedStream *typedStream;
  172.     int i;
  173.  
  174.     typedStream = NXOpenTypedStreamForFile(scoreFileName, NX_WRITEONLY);
  175.     if (!typedStream) {
  176.         NXRunAlertPanel("writeHighScores","Cannot save high scores.",
  177.             NULL, NULL, "OK");
  178.         return self;
  179.     }
  180.     for (i=0; i<10; i++) {
  181.         NXWriteTypes(typedStream, "i*", &highScores[i], &highNames[i]);
  182.     }
  183.     NXCloseTypedStream(typedStream);
  184.     return self;
  185. }
  186.  
  187. - clearHighScores:sender    // zeroes the high scores -- IB method
  188. {
  189.     [self zeroHighScores];
  190.     [[NXApp delegate] showHigh]; // make game brain update "high score" field
  191.     return [self writeHighScores];
  192. }
  193.  
  194. - acceptHighScore:sender    // accept name into high score table - sent by
  195. {                // name matrix when user fills in his/her name
  196.     int i;
  197.     
  198.     if (highCell != nil) { // should always be true, but check just in case
  199.         i = [highCell tag]/2;
  200.         strcpy(highNames[i], [highCell stringValue]);
  201.         strcpy(defaultPlayerName, highNames[i]);
  202.         [preferences setDefaultPlayerName:defaultPlayerName];
  203.         [highCell setBackgroundGray:NX_LTGRAY];
  204.         [highCell setEditable:NO];
  205.         [highCell setBezeled:NO];
  206.         highCell = nil;
  207.     }
  208.     [self writeHighScores];    // be sure that highscore file is updated
  209.     [gameWindow makeKeyAndOrderFront:self];
  210.     return self;
  211. }
  212.  
  213. @end
  214.